package com.baosight.sczx.zm.ch.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHMineReport;

/**
 * 采煤进度汇报
 * <p>Title: ServiceZMCH00.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-6 下午02:24:59  张志芳  Create
 *</pre>
 */
public class ServiceZMCH00 extends ServiceEPBase {
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:53:52 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:03 张志芳
	 *    2016-8-29 15:20:45  王伟修改，添加采煤工程选择框
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String departmentId = (String)UserSession.getInSessionProperty("orgCode"); 
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdReportDepartmentId", departmentId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCHMineReport.query", new ZMCHMineReport());
		
	    
		outInfo.addBlock(Engineering.getEngineeringForCM("cmes.cp.engineering", "0", "", "1", true,fdMiningId,departmentId));
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
//		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNumInqu", true));
		outInfo.addBlock(StaticBlocks.getStauts4());//状态
		outInfo.set("userId", UserSession.getUserId());
		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("departmentId", departmentId);
		outInfo.set("departmentName", UserSession.getInSessionProperty("orgName"));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdMiningId", fdMiningId);//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:15 张志芳
	 * </p>
	 *
	 */ 
	@Override
	public EiInfo insert(EiInfo inInfo) {	
		EiInfo out=new EiInfo();
		ZMCHMineReport _dom = new ZMCHMineReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;		
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			String fdStatus = "草拟";
			boolean validate = validate(inInfo, _dom,"ZMCHMineReport.validate",params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			else {
				_dom.setFdStatus(fdStatus);
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_MINEREPORT"));//主键
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
				//_dom.setFdCurrentDate(StaticTools.getDate());// 当前日期
				_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 汇报部门id
				_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
				_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
				_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人				
				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
				this.dao.insert("ZMCHMineReport.insert",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 	
		
		
		
//		EiInfo out=new EiInfo();
//		ZMCHMineReport _dom = new ZMCHMineReport();
//		EiBlock eiBlock = inInfo.getBlock("result");
//		eiBlock.setBlockMeta(_dom.eiMetadata);
//		int rowcount = eiBlock.getRowCount();
//		int i = 0;		
//		for (; i < rowcount; i++) {
//			_dom.fromMap(eiBlock.getRow(i));
//			if (!validate(_dom)) {
//				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_MINEREPORT"));//主键
//				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
//				this.dao.insert("ZMCHMineReport.insert",_dom);
//				if("1".equals(_dom.getFdStatus()))
//				{}
//			}
//			else {
//				out = this.query(inInfo);
//				out.setStatus(-1);
//				out.setMsg(_dom.getFdCurrentDate()+"该班次的采矿进度已经填写！"); 
//				return out;
//			}
//		}
//		out = this.query(inInfo);
//		out.setStatus(1);
//		out.setMsg("操作" + rowcount + "记录成功");
//		return out; 
//		
//		 

} 

	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:24 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHMineReport _dom = new ZMCHMineReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = validate(inInfo, _dom,"ZMCHMineReport.validate",params);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}
			else {
//				_dom.setFdStatus("草拟");
				this.dao.update("ZMCHMineReport.update",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
			
	}		
			
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCHMineReport _dom = new ZMCHMineReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCHMineReport.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHMineReport _dom = new ZMCHMineReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdStatus("上报");
			dao.update("ZMCHMineReport.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   	
	}
	

	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-16下午12:48:34 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCHMineReport _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdWorknumId = _dom.getFdWorknumId();
		String fdMiningId = _dom.getFdMiningId();
		String fdCurrentDate = _dom.getFdCurrentDate();
		params.put("fdWorknumId", fdWorknumId);
		params.put("fdMiningId", fdMiningId);
		params.put("fdCurrentDate", fdCurrentDate);
		List result = dao.query("ZMCHMineReport.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "采煤进度汇报每单位每天每班只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	
	}	
//	private boolean validate(ZMCHMineReport _dom) {
//		Map<String, String> params = new HashMap<String, String>();
//		String fdWorknumId = _dom.getFdWorknumId();
//		String fdCurrentDate = _dom.getFdCurrentDate();
//		String fdKeyId = _dom.getFdKeyId();
//		params.put("fdWorknumId", fdWorknumId);
//		params.put("fdCurrentDate", fdCurrentDate);
//		params.put("fdKeyId", fdKeyId);
//		if(fdKeyId!=null&&!fdKeyId.trim().equals(""))
//			params.put("fdKeyId", fdKeyId);
//	
//		List result = dao.query("ZMCHMineReport.validate", params);
//		if (result != null && result.size() > 0) {
//			return true;
//		}
//		return false;
	
}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	